package com.yang.frost.blueberry.quartz.controller;

import com.yang.frost.blueberry.quartz.entity.form.JobForm;
import com.yang.frost.blueberry.quartz.service.JobService;
import com.yang.frost.blueberry.util.ApiResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.quartz.SchedulerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author 10296
 * @date 2021/1/20
 * @Description:
 */
@RestController
@RequestMapping("/jobApi")
@Api(value = "定时任务API", tags = {"定时任务"})
public class JobController {
    private Logger logger = LoggerFactory.getLogger(JobController.class);


    @Autowired
    private JobService jobService;

    @PostMapping("/addJob")
    @ApiOperation(value = "添加定时任务", notes = "添加定时任务")
    public ApiResult addJob(@RequestBody JobForm form) {
        try {
            jobService.addJob(form);
        } catch (Exception e) {
            logger.error("【定时任务】添加失败！", e);
            e.printStackTrace();
            return ApiResult.fail("【定时任务】添加失败！");
        }
        return ApiResult.ok("操作成功!");
    }

    @PostMapping("/deleteJob")
    @ApiOperation(value = "删除定时任务", notes = "删除定时任务")
    public ApiResult deleteJob(@RequestBody JobForm jobForm) {
        if (StringUtils.isEmpty(jobForm.getJobClassName()) || StringUtils.isEmpty(jobForm.getJobGroupName())) {
            return ApiResult.fail("参数不能为空！");
        }

        try {
            jobService.deleteJob(jobForm);
        } catch (Exception e) {
            logger.error("【定时任务】删除失败", e);
            e.printStackTrace();
            return ApiResult.fail("删除失败！");
        }
        return ApiResult.ok("删除成功");

    }

    @PostMapping("/pauseJob")
    @ApiOperation(value = "暂停定时任务", notes = "暂停定时任务")
    public ApiResult pauseJob(@RequestBody JobForm jobForm) {
        if (StringUtils.isEmpty(jobForm.getJobClassName()) || StringUtils.isEmpty(jobForm.getJobGroupName())) {
            return ApiResult.fail("参数不能为空！");
        }
        try {
            jobService.pauseJob(jobForm);
        } catch (SchedulerException e) {
            logger.error("【定时任务】暂停失败！");
            e.printStackTrace();
            return ApiResult.fail("定时任务暂停失败！");

        }
        return ApiResult.ok("暂停成功");
    }

    @PostMapping("/resumeJob")
    @ApiOperation(value = "恢复定时任务", notes = "恢复定时任务")
    public ApiResult resumeJob(@RequestBody JobForm jobForm) {
        if (StringUtils.isEmpty(jobForm.getJobClassName()) || StringUtils.isEmpty(jobForm.getJobGroupName())) {
            return ApiResult.fail("参数不能为空！");
        }
        try {
            jobService.resumeJob(jobForm);
        } catch (SchedulerException e) {
            logger.error("【定时任务】恢复失败！", e);
            e.printStackTrace();
            return ApiResult.fail("恢复失败");

        }
        return ApiResult.ok("定时任务恢复成功！");

    }

    @PostMapping("/cronJob")
    @ApiOperation(value = "修改定时任务", notes = "修改定时任务")
    public ApiResult cronJob(@RequestBody JobForm jobForm) {
        try {
            jobService.cronJob(jobForm);
        } catch (Exception e) {
            logger.error("【定时任务】修改失败！", e);
            e.printStackTrace();
            return ApiResult.fail("修改失败");
        }
        return ApiResult.ok("修改成功！");

    }

    @PostMapping("/qryJobList")
    @ApiOperation(value = "查询定时任务", notes = "查询定时任务")
    public ApiResult qryJobList(@RequestBody Map<String, Object> paramsMap) {
//        MapUtils.getIntValue(paramsMap,"")
//        if ()
        return jobService.list(paramsMap);


    }


}
